package com.qiaofang.collectmanage.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.qiaofang.collectmanage.dao.OperatorLogEsDao;
import com.qiaofang.collectmanage.dao.elasticsearch.EsClient;
import com.qiaofang.collectmanage.dao.util.OperatorLogDaoConvert;
import com.qiaofang.collectmanage.stub.bean.SaveOperatorLogDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository("operatorLogEsDao")
public class OperatorLogEsDaoImpl implements OperatorLogEsDao {

    private static Logger logger = LoggerFactory.getLogger(OperatorLogEsDaoImpl.class);

    @Override
    public int saveOperatorLog(String indexHswhHouseAgent, String indexTypeAgent, List<SaveOperatorLogDTO> saveOperatorLogDTOS) {
        List<JSONObject> jsonObjects = saveOperatorLogDTOS.stream().map(OperatorLogDaoConvert::convertOperatorLogDtoToJSONObjectCollect).collect(Collectors.toList());
        try {
            EsClient.addDoBulk(indexHswhHouseAgent,indexTypeAgent,"logUuid",jsonObjects);
            return 1;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            logger.error("操作日志保存失败{}",e);
        }
        return 0;
    }
}
